//
//  main.m
//  Demo0022
//
//  Created by zTercel on 13-3-4.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>


int main(int argc, const char * argv[])
{
    @autoreleasepool {
        NSString* testFile      = @"testFile.txt";
        NSString* copyFile      = @"copyFile.txt";
        NSString* lastFile      = @"lastFile.txt";
        NSString* dataContent   = @"this is a test file, i create it at 2013.3.4";
        
        //NSFileManager* fm = [NSFileManager defaultManager];
        NSFileManager* fm = [[NSFileManager alloc]init];
        if ([fm fileExistsAtPath:testFile])
        {
            NSLog(@"file existing, will delete it");
            [fm removeItemAtPath:testFile error:nil];
        }
        
        NSData* fileContent = [NSData dataWithBytes:[dataContent UTF8String] length:[dataContent length]];
        if ([fm createFileAtPath:testFile contents:fileContent attributes:nil])
        {
            NSLog(@"create file at %@", [fm currentDirectoryPath]);
            
        }
        
        if ([fm copyItemAtPath:testFile toPath:copyFile error:nil])
        {
            NSString* fileName = [fm currentDirectoryPath];
            NSLog(@"copy file to %@", [fileName stringByAppendingPathComponent:copyFile]); //[fm fileSystemRepresentationWithPath:copyFile]);
        }
        
        NSData* fileData = [fm contentsAtPath:copyFile];
        NSLog(@"file content: [%s]", [fileData bytes]);
        
        if ([fm contentsEqualAtPath:testFile andPath:copyFile])
        {
            NSLog(@"%@ content equal to %@", testFile, copyFile);
        }
        
        if ([fm moveItemAtPath:copyFile toPath: lastFile error:nil])
        {
            NSLog(@"rename file %@ to %@", copyFile, lastFile);
        }

        NSDictionary* attributes = [fm attributesOfItemAtPath:lastFile error:nil];
        NSLog(@"%@ file attributes: %@", lastFile, attributes);
        
        if ([fm removeItemAtPath:lastFile error:nil])
        {
            NSLog(@"remove file %@", lastFile);
        }
        [fm removeItemAtPath:testFile error:nil];
    
    }
    return 0;
}

